<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      v-model="form"
      :data="data"
      :page.sync="page"
      :search.sync="query"
      :permission="permissionList"
      :before-open="beforeOpen"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
    </avue-crud>
  </basic-container>
</template>
<script>
import { mapGetters } from 'vuex';
import { getAqlConfigList, removeConfig, saveOrUpdateConfig } from '@/api/quality/aqlConfig';
export default {
  name: 'AQLconfig',
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      data: [],
      option: {
        height: 'calc(100vh - 440px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 4,
        searchLabelWidth: 150,
        labelWidth: 150,
        border: true,
        index: true,
        viewBtn: true,
        editBtn: true,
        delBtn: true,
        addBtn: true,
        indexLabel: '序号',
        columnBtn: false,
        column: [
          {
            label: 'AQL配置方案编码',
            prop: 'code',
            search: true,
            disabled: true
          },
          {
            label: 'AQL配置方案名称',
            prop: 'name',
            search: true,
            rules: [{ required: true, message: '请输入AQL配置方案名称', trigger: 'blur' }]
          },
          {
            label: '抽样标准类型',
            prop: 'samplingStandard',
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=sample_type',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            },
            search: true,
            rules: [{ required: true, message: '请选择抽样标准类型', trigger: 'blur' }],
            control: (val, form) => {
              if (val == 'MIL-STD-1916 C-0') {
                return {
                  severityCode: {
                    display: false
                  },
                  levelType: {
                    display: false
                  },
                  levelCode: {
                    display: false
                  }
                };
              } else {
                return {
                  severityCode: {
                    display: true
                  },
                  levelType: {
                    display: true
                  },
                  levelCode: {
                    display: true
                  }
                };
              }
            }
          },
          {
            label: '严格程度',
            prop: 'severityCode',
            type: 'select',
            search: true,
            dicUrl: '/api/blade-system/dict/dictionary?code=sample_plan',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            },
            rules: [
              {
                required: true,
                message: '请选择严格程度',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '检验水平',
            prop: 'levelType',
            type: 'select',
            search: true,
            display: true,
            dicUrl: '/api/blade-system/dict/dictionary?code=aql_inspection_level',
            cascader: ['levelCode'],
            props: {
              label: 'dictValue',
              value: 'dictKey'
            },
            control: (val, form) => {
              if (val == 'SPECIAL') {
                return {
                  levelCode: { dicUrl: '/api/blade-system/dict/dictionary?code=special_inspection_level' }
                };
              } else {
                return {
                  levelCode: { dicUrl: '/api/blade-system/dict/dictionary?code=general_inspection_level' }
                };
              }
            },
            rules: [
              {
                required: true,
                message: '请输入检验水平',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '检验水平代码',
            prop: 'levelCode',
            type: 'select',
            // cascader: ['sampleSizeCode'],
            display: true,
            props: {
              label: 'dictValue',
              value: 'dictKey'
            },
            dicData: [
              {
                dictValue: 'Ⅰ',
                dictKey: '1'
              },
              {
                dictValue: 'Ⅱ',
                dictKey: '2'
              },
              {
                dictValue: 'Ⅲ',
                dictKey: '3'
              }
            ],
            // control: (val, form) => {
            //   switch (val) {
            //     case 'S-1':
            //       return {
            //         sampleSizeCode: {
            //           dicFormatter: (res) => {
            //             return res.data.slice(0, 4);
            //           }
            //         }
            //       };
            //     case 'S-2':
            //       return {
            //         sampleSizeCode: {
            //           dicFormatter: (res) => {
            //             return res.data.slice(0, 5);
            //           }
            //         }
            //       };
            //     case 'S-3':
            //       return {
            //         sampleSizeCode: {
            //           dicFormatter: (res) => {
            //             return res.data.slice(0, 8);
            //           }
            //         }
            //       };
            //     case 'S-4':
            //       return {
            //         sampleSizeCode: {
            //           dicFormatter: (res) => {
            //             return res.data.slice(0, 10);
            //           }
            //         }
            //       };
            //     case '1':
            //       return {
            //         sampleSizeCode: {
            //           dicFormatter: (res) => {
            //             return res.data.slice(0, 13);
            //           }
            //         }
            //       };
            //     case '2':
            //       return {
            //         sampleSizeCode: {
            //           dicFormatter: (res) => {
            //             return res.data.slice(0, 15);
            //           }
            //         }
            //       };
            //     case '3':
            //       return {
            //         sampleSizeCode: {
            //           dicFormatter: (res) => {
            //             return res.data.slice(1, 16);
            //           }
            //         }
            //       };
            //     default:
            //       break;
            //   }
            // },
            rules: [
              {
                required: true,
                message: '请输入检验水平代码',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '质量水平代码',
            prop: 'limitCode',
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=sample_code',
            props: {
              label: 'dictKey',
              value: 'dictKey'
            },
            dicFormatter: (data) => {
              return this._map(data.data, (item) => {
                return {
                  ...item,
                  dictKey: item.dictKey
                };
              });
            },
            rules: [
              {
                required: true,
                message: '请选择质量水平代码',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '严重缺陷质量代码',
            prop: 'severityLimitCode',
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=sample_code',
            props: {
              label: 'dictKey',
              value: 'dictKey'
            },
            hide: true
          },
          {
            label: '主要缺陷质量代码',
            prop: 'majorLimitCode',
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=sample_code',
            props: {
              label: 'dictKey',
              value: 'dictKey'
            },
            hide: true
          },
          {
            label: '次要缺陷质量代码',
            prop: 'minorLimitCode',
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=sample_code',
            props: {
              label: 'dictKey',
              value: 'dictKey'
            },
            hide: true
          },
          {
            label: '是否启用',
            prop: 'enabled',
            type: 'switch',
            value: true,
            dicData: [
              {
                label: '否',
                value: false
              },
              {
                label: '是',
                value: true
              }
            ]
          },
          // {
          //   label: '是否默认',
          //   prop: 'isDefault',
          //   type: 'switch',
          //   value: false,
          //   dicData: [
          //     {
          //       label: '否',
          //       value: false
          //     },
          //     {
          //       label: '是',
          //       value: true
          //     }
          //   ]
          // },
          {
            label: '方案描述',
            prop: 'description',
            type: 'textarea',
            hide: true,
            span: 24
          },
          {
            label: '更新人',
            prop: 'updateUserName',
            display: false
          },
          {
            label: '更新时间',
            prop: 'updateTime',
            display: false
          }
        ]
      }
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.AQLconfig_add, false),
        viewBtn: this.vaildData(this.permission.AQLconfig_view, false),
        delBtn: this.vaildData(this.permission.AQLconfig_del, false),
        editBtn: this.vaildData(this.permission.AQLconfig_edit, false)
      };
    }
  },
  methods: {
    async rowSave(row, done, loading) {
      try {
        await saveOrUpdateConfig(row);
        this.$message.success('保存成功');
        this.onLoad(this.page);
        done();
      } catch (e) {
        loading();
      }
    },
    async rowUpdate(row, index, done, loading) {
      try {
        await saveOrUpdateConfig(row);
        this.$message.success('保存成功');
        this.onLoad(this.page);
        done();
      } catch (e) {
        loading();
      }
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeConfig({ ids: row.id });
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
    },
    beforeOpen(done, type) {
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    async onLoad(page, params = {}) {
      this.loading = true;
      try {
        const res = await getAqlConfigList(page.currentPage, page.pageSize, Object.assign(params, this.query));
        const data = res.data.data;
        this.data = data.records;
        this.page.total = data.total;
      } catch (e) {
        console.warn(e);
      } finally {
        this.loading = false;
      }
    }
  }
};
</script>
<style lang="scss" scoped></style>
